package com.simple.editor.ui;

import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vsoroka
 * Date: 03.06.13
 * Time: 10:14
 * To change this template use File | Settings | File Templates.
 */
class FileSystemTreeModel implements TreeModel {
    private final FileModel root;
    private final List<TreeModelListener> listeners = new ArrayList<TreeModelListener>();

    public FileSystemTreeModel(final FileModel rootDirectory) {
        root = rootDirectory;
    }

    public Object getRoot() {
        return root;
    }

    public Object getChild(final Object parent, final int index) {
        final FileModel directory = (FileModel) parent;
        final String[] children = directory.list();
        return new FileModel(directory, children[index]);
    }

    public int getChildCount(final Object parent) {
        int count = 0;
        final FileModel file = (FileModel) parent;
        if (file.isDirectory()) {
            final String[] fileList = file.list();
            if (fileList != null) {
                count = file.list().length;
            }
        }
        return count;
    }

    public boolean isLeaf(final Object node) {
        final FileModel file = (FileModel) node;
        return file.isFile();
    }

    public int getIndexOfChild(final Object parent, final Object child) {
        int childIndex = -1;
        final FileModel directory = (FileModel) parent;
        final FileModel file = (FileModel) child;
        final String[] children = directory.list();
        for (int i = 0; i < children.length; i++) {
            if (file.getName().equals(children[i])) {
                childIndex = i;
                break;
            }
        }
        return childIndex;

    }

    public void valueForPathChanged(final TreePath path, final Object value) {
        final FileModel oldFile = (FileModel) path.getLastPathComponent();
        final String fileParentPath = oldFile.getParent();
        final String newFileName = (String) value;
        final FileModel targetFile = new FileModel(fileParentPath, newFileName);
        oldFile.renameTo(targetFile);
        final FileModel parent = new FileModel(fileParentPath);
        final int[] changedChildrenIndices = {getIndexOfChild(parent, targetFile)};
        final Object[] changedChildren = {targetFile};
        fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);

    }

    private void fireTreeNodesChanged(final TreePath parentPath, final int[] indices, final Object[] children) {
        final TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
        final Iterator iterator = listeners.iterator();
        TreeModelListener listener;
        while (iterator.hasNext()) {
            listener = (TreeModelListener) iterator.next();
            listener.treeNodesChanged(event);
        }
    }

    public void addTreeModelListener(final TreeModelListener listener) {
        listeners.add(listener);
    }

    public void removeTreeModelListener(final TreeModelListener listener) {
        listeners.remove(listener);
    }
}
